﻿using MediatR;
using TaskProject.Application.Command.Production;
using TaskProject.Domain.Production;
using TaskProject.ErrorCode;
using TaskProject.Extension;
using TaskProject.InfrasIntructure.Interface;

namespace TaskProject.Application.Handler.Production
{
    public class GetWorkOrderHandler : IRequestHandler<GetWorkOrderCommand, APIResult<List<WorkOrderDTO>>>
    {
        IBaseRepository<WorkorderModel> workRepo;
        private readonly IBaseRepository<LeaderModel> leaderRepo;
        private readonly IBaseRepository<ProductItemModel> productRepo;

        public GetWorkOrderHandler(IBaseRepository<WorkorderModel> workRepo, IBaseRepository<LeaderModel> leaderRepo, IBaseRepository<ProductItemModel> productRepo)
        {
            this.workRepo = workRepo;
            this.leaderRepo = leaderRepo;
            this.productRepo = productRepo;
        }

        public  Task<APIResult<List<WorkOrderDTO>>> Handle(GetWorkOrderCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<WorkOrderDTO>> res=new APIResult<List<WorkOrderDTO>>();
            var works=workRepo.GetAll();
            var leader = leaderRepo.GetAll();
            var product = productRepo.GetAll();
            var list = from a in works
                       join b in leader
                       on a.LeaderId equals b.LeaderId
                       join c in product
                       on a.ProductId equals c.ProductId
     
                       select new WorkOrderDTO
                       {
                           WorkOrderId=a.WorkOrderId,
                           WorkOrderCode   =a.WorkOrderCode    ,
                           WorkOrderName   =a. WorkOrderName   ,
                         
                           LeaderId        =a. LeaderId        ,
                           LeaderName      =b. LeaderName      ,
                           OrderNum        =a. OrderNum        ,
                           WorderOrderDesc =a. WorderOrderDesc ,
                           WorkOrderDate   =a. WorkOrderDate   ,
                           ProductId       =a. ProductId       ,
                           ProductName     =c. ProductName     ,
                       };

            res.Msg = "查询生产工单成功";
            res.Code=APIEnum.Success;
            res.Data =list.ToList();
            return Task.FromResult(res);
        }

     
    }
}
